<?php

namespace AdminBundle\Repository;

use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\ORM\EntityRepository;

abstract class BaseRepository extends EntityRepository
{
    const PAGE_SIZE = 12;

    /**
     * 执行无返回结果的原生SQL语句,例:update,insert,delete,load data...
     * @param $sql
     * @param null $params
     * @return bool
     * @throws \Exception
     */
    protected function rawExecute($sql, $params = null)
    {
        $conn = $this->getEntityManager()->getConnection();
        try {
            $stmt = $conn->prepare($sql);
            if ($stmt->execute($params)) {
                return $stmt->rowCount();
            }
        } catch (\Exception $e) {
            throw $e;
        } finally {
            unset($stmt);
        }
        return false;
    }

    /**
     * 返回结果的原生SQL语句,例:select .
     * @param $sql
     * @param null $params
     * @return bool
     * @throws \Exception
     */
    protected function rawQuery($sql, $params = null)
    {
        $conn = $this->getEntityManager()->getConnection();
        try {
            $stmt = $conn->prepare($sql);
            if ($stmt->execute($params))
                return $stmt->fetchAll();
        } catch (\Exception $e) {
            throw $e;
        } finally {
            unset($stmt);
        }
        return false;
    }

    protected function rawSql($sql, $params = null)
    {
        return $this->rawQuery($sql, $params);
    }

    /**
     * doctrine方式翻页方法
     * @param $sql
     * @param $param
     * @param $pageIndex
     * @param int $pageSize
     * @return array
     */
    public function pageHelperForDoctrine($sql, $param, $pageIndex, $pageSize = self::PAGE_SIZE)
    {
        $offset = $pageIndex * $pageSize - $pageSize;
//        $maxResult = $pageIndex * $pageSize + 1;
        $list = $this->getEntityManager()->createQuery($sql)
            ->setParameters($param)
            ->setFirstResult($offset)
            ->setMaxResults($pageSize + 1)
            ->getResult();
        return $this->pageTemplate($list, $pageIndex, $offset, $pageSize);
    }

    /**
     * 是否有下一页
     * @param $list
     * @param $pageSize
     * @return bool
     */
    public function hasNextPage(&$list, $pageSize)
    {
        $rowcount = count($list);
        if ($rowcount > $pageSize) {
            array_pop($list);//去掉多取的那条
            return $rowcount - 1;
        }
        return $rowcount;
    }

    /**
     * 原生方式翻页
     * @param $sql
     * @param $param
     * @param $pageIndex
     * @param int $pageSize
     * @return array
     * @throws \Doctrine\DBAL\DBALException
     */
    public function pageWithRawSql($sql, $param, $pageIndex, $pageSize = self::PAGE_SIZE)
    {
        $pageIndex = (int)$pageIndex;
        $offset = $pageIndex * $pageSize - $pageSize;
        $sql .= sprintf(' limit %d , %d', $offset, $pageSize + 1);
        $list = $this->rawQuery($sql, $param);

        return $this->pageTemplate($list, $pageIndex, $offset, $pageSize);
    }

    public function pageTemplate($list = [], $pageIndex = 1, $offset = 0, $pageSize = self::PAGE_SIZE)
    {
        $rows = $this->hasNextPage($list, $pageSize);
        return [
            "list" => $list,
            'page' => [
                "hasNext" => $rows == $pageSize,
                'index' => $pageIndex, 'size' => $pageSize,
                'offset' => $offset, 'rows' => $rows]
        ];
    }
}